﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.business.stamm;
using personalplaner.core.sql;
using personalplaner.business.common;
using personalplaner.core.errorhandling;

namespace personalplaner.dataprovider.services
{
	public partial class PensumDataService
	{
		#region Get Methods

		/// <summary>
		/// gets a <see cref="Pensum"/> of a Person with stichdatum today. prsid,von,bis,pensum 
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="prsid">prsid</param>
		/// <returns>prsid,von,bis,pensum </returns>
		public Pensum GetPensum(DatabaseSettings settings, int prsid)
		{
			return GetPensum(settings, prsid, DateTime.Now);
		}

		/// <summary>
		/// Creates a DataView with the Pensum of a Person at stichdatum. prsid,von,bis,pensum 
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="prsid">prsid</param>
		/// <param name="stichdatum">stichdatum</param>
		/// <returns>prsid,von,bis,pensum </returns>
		public Pensum GetPensum(DatabaseSettings settings, int prsid, DateTime stichdatum)
		{
			var query = new Querry(settings, "get_personpensum");
			query.AddParameter("@prsid", prsid);
			query.AddParameter("@stichdatum", stichdatum);
			var table = query.ExecuteForDataTable();

			if (table.Rows.Count == 0)
				return null;

			try
			{
				return m_pensumData(table);
			}
			catch (Exception e)
			{
				throw new Exception("GetPensum caused an error", e);
			}
		}

		public IEnumerable<Pensum> GetPensen(DatabaseSettings settings, int prsid)
		{
            var str = string.Format("select prsid = PrsID, von = Von, bis = Bis, pensum = Prozent, status = Status, statustext = case when Status=1 then 'Aktiv' else 'Inaktiv' end from Arbeitspensum where PrsId = {0} order by Bis desc", prsid);
			var table = SimpleQuerry.ExecuteForDataTable(settings, str);

			if (table.Rows.Count == 0)
				return null;

			try
			{
				return m_pensenData(table);
			}
			catch (Exception e)
			{
				throw new Exception("GetPersonPensen caused an error", e);
			}
		}

		/// <summary>
		/// ehemals GetArbeitspensum
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="prsid"></param>
		/// <returns></returns>
		public int GetPensumProzent(DatabaseSettings settings, int prsid)
		{
			var dv = GetPensum(settings, prsid);
			if (dv != null)
				return dv.Prozent;
			return 100;
		}

		/// <summary>
		/// ehemals GetArbeitspensum
		/// </summary>
		/// <param name="settings"></param>
		/// <param name="prsid"></param>
		/// <param name="stichdatum"></param>
		/// <returns></returns>
		public int GetPensumProzent(DatabaseSettings settings, int prsid, DateTime stichdatum)
		{
			var dv = GetPensum(settings, prsid, stichdatum);
			if (dv != null)
				return dv.Prozent;
			return 0;
		}

		#endregion

		#region Set Methods

		public bool SetPensum(DatabaseSettings settings, int prsid, int pensum, DateTime von, DateTime bis)
		{
			ActionLogger.LoggMessage(MessageType.SQL, "Pensum ändern von Person:" + prsid + " Prozent:" + pensum + " Von:" + von + " Bis:" + bis);

			var stp = new Querry(settings, "set_newpensum");

			stp.AddParameter("@prsid", prsid);
			stp.AddParameter("@pensum", pensum);
			stp.AddParameter("@von", von.ToString("yyyyMMdd"));
			stp.AddParameter("@bis", bis.ToString("yyyyMMdd"));

			return stp.Execute(true);
		}

		public bool SetKuendigung(DatabaseSettings settings, int prsid, DateTime datum)
		{
			ActionLogger.LoggMessage(MessageType.SQL, "Kündigung von Person:" + prsid + " Am:" + datum);

			var stp = new Querry(settings, "set_personkuendigung");

			stp.AddParameter("@prsid", prsid);
			stp.AddParameter("@bis", datum.ToString("yyyyMMdd"));

			return stp.Execute(true);
		}

		#endregion

		#region Delete Methods

        public bool DelPensum(DatabaseSettings settings, Pensum pensum)
        {
            ActionLogger.LoggMessage(MessageType.SQL, "Deleting pensum from Person:" + pensum.PrsID);

            var stp = new Querry(settings, "del_pensum");

            stp.AddParameter("@prsid", pensum.PrsID);
            stp.AddParameter("@von", pensum.Von.ToString("yyyyMMdd"));
            stp.AddParameter("@bis", pensum.Bis.ToString("yyyyMMdd"));
            stp.AddParameter("@pensum", pensum.Prozent);

            return stp.Execute();
        }

		#endregion
	}
}
